// DlgResize.cpp : implementation file
//

#include "stdafx.h"
#include "PhotoEdit.h"
#include "DlgResize.h"
#include "afxdialogex.h"


// CDlgResize dialog

IMPLEMENT_DYNAMIC(CDlgResize, CDialogEx)

CDlgResize::CDlgResize(CWnd* pParent /*=NULL*/)
	: CDialogEx(CDlgResize::IDD, pParent)
	, m_isPreserve(FALSE)
	, m_width(0)
	, m_height(0)
{

}
CDlgResize::CDlgResize(CDlgHomeTab* pParent)
	: CDialogEx(CDlgResize::IDD, pParent)
{
	m_pHomeTab = pParent;
	m_isPreserve = TRUE;
	//m_isEditWidth = TRUE;
	m_width = m_pHomeTab->m_pPhoto->GetWidth();
	m_height = m_pHomeTab->m_pPhoto->GetHeight();
	m_ratio = (float)m_width/m_height;

}
CDlgResize::~CDlgResize()
{
}

void CDlgResize::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Check(pDX, IDC_CHECK_PRESERVE, m_isPreserve);
	DDX_Text(pDX, IDC_EDIT_WIDTH, m_width);
	DDV_MinMaxInt(pDX, m_width, 0, 8192);
	DDX_Text(pDX, IDC_EDIT_HEIGHT, m_height);
	DDV_MinMaxInt(pDX, m_height, 1, 8192);
	DDX_Control(pDX, IDC_EDIT_WIDTH, m_ctrWidth);
	DDX_Control(pDX, IDC_EDIT_HEIGHT, m_ctrHeight);
	DDX_Control(pDX, IDC_CHECK_PRESERVE, m_ctrCheckPreserve);
}


BEGIN_MESSAGE_MAP(CDlgResize, CDialogEx)
	ON_BN_CLICKED(IDOK, &CDlgResize::OnBnClickedOk)
	ON_EN_CHANGE(IDC_EDIT_WIDTH, &CDlgResize::OnEnChangeEditWidth)
	ON_EN_CHANGE(IDC_EDIT_HEIGHT, &CDlgResize::OnEnChangeEditHeight)
	ON_EN_SETFOCUS(IDC_EDIT_WIDTH, &CDlgResize::OnEnSetfocusEditWidth)
	ON_EN_SETFOCUS(IDC_EDIT_HEIGHT, &CDlgResize::OnEnSetfocusEditHeight)
	ON_BN_CLICKED(IDC_CHECK_PRESERVE, &CDlgResize::OnBnClickedCheckPreserve)
END_MESSAGE_MAP()


// CDlgResize message handlers
BOOL CDlgResize::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	
	//CRichEditCtrl().SetEventMask(ENM_CHANGE);
	//CRichEditCtrl().SetEventMask(ENM_CHANGE | CRichEditCtrl().GetEventMask());
	
	m_ctrWidth.SetLimitText(4);
	m_ctrHeight.SetLimitText(4);
	return TRUE; 
}

void CDlgResize::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	CDialogEx::OnOK();
	m_pHomeTab->Resize(m_width,m_height);

}


void CDlgResize::OnEnChangeEditWidth()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialogEx::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	// TODO:  Add your control notification handler code here
	if(!(m_ctrCheckPreserve.GetCheck() && m_isEditWidth))
		return;
	TCHAR str[10];
	m_ctrWidth.GetWindowText(str,9);
	int w = _ttoi(str);
	int h = (int)(w/m_ratio+0.5);
	_itot_s(h,str,10);
	m_ctrHeight.SetWindowText(str);
}


void CDlgResize::OnEnChangeEditHeight()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialogEx::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	// TODO:  Add your control notification handler code here
	//BOOL f = m_ctrHeight.is
	if(!m_ctrCheckPreserve.GetCheck() || m_isEditWidth)
		return;
	TCHAR str[10];
	m_ctrHeight.GetWindowText(str,9);
	int h = _ttoi(str);
	int w = (int)(h*m_ratio+0.5);
	_itot_s(w,str,10);
	m_ctrWidth.SetWindowText(str);
}


void CDlgResize::OnEnSetfocusEditWidth()
{
	// TODO: Add your control notification handler code here
	m_isEditWidth = TRUE;
}


void CDlgResize::OnEnSetfocusEditHeight()
{
	// TODO: Add your control notification handler code here
	m_isEditWidth = FALSE;
}


void CDlgResize::OnBnClickedCheckPreserve()
{
	// TODO: Add your control notification handler code here
	if(m_ctrCheckPreserve.GetCheck())
	{
		TCHAR str[10];
		m_ctrWidth.GetWindowText(str,9);
		int w = _ttoi(str);
		int h = (int)(w/m_ratio+0.5);
		_itot_s(h,str,10);
		m_ctrHeight.SetWindowText(str);
	}
}
